package services.midas.assembler;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class Assembler {
	public static Map<Long,String> jsonArray2Map(JSONArray array) throws JSONException{
		Map<Long,String> result = new HashMap<Long,String>();
		for(int i = 0; i < array.length(); i++){
			JSONObject element = array.getJSONObject(i);
			result.put(element.getLong("id"), element.getString("value"));
		}
		
		return result;
	}
	
	public static String collection2String(Collection data,String Separator){
		StringBuffer result = new StringBuffer();
		for(Object o : data){
			result.append(o);
			result.append(Separator);
		}
		if(result.lastIndexOf(Separator) != -1){
			result.deleteCharAt(result.lastIndexOf(Separator));
		}
		return result.toString();
	}
}
